package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.*;
import cn.tedu.csmall.product.pojo.dto.SpuAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.Spu;
import cn.tedu.csmall.product.pojo.entity.SpuDetail;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.ISpuService;
import cn.tedu.csmall.product.util.IdUtils;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SpuServiceImpl implements ISpuService {

    @Autowired
    private SpuMapper mapper;
    @Autowired
    private SpuDetailMapper detailMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private AlbumMapper albumMapper;

    @Override
    public void addNew(SpuAddNewDTO addNewDTO) {
        log.debug("开始处理【新增SPU】的业务，参数：{}", addNewDTO);

        AlbumStandardVO albumStandardVO = albumMapper.getStandardById(addNewDTO.getAlbumId());
        if (albumStandardVO == null) {
            String message = "添加spu失败，该相册不存在!";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        BrandStandardVO brandStandardVO = brandMapper.getStandardById(addNewDTO.getBrandId());
        if (brandStandardVO == null) {
            String message = "添加spu失败，该品牌不存在!";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }else if (brandStandardVO.getEnable()==0){
            String message = "添加spu失败，该品牌已被禁用!";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        CategoryStandardVO categoryStandardVO = categoryMapper.getStandardById(addNewDTO.getCategoryId());
        if (categoryStandardVO == null) {
            String message = "添加spu失败，该类别不存在!";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }else if (categoryStandardVO.getEnable()==0){
            String message = "添加spu失败，该类别已被禁用!";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }else if (categoryStandardVO.getIsParent()==1){
            String message = "新增SPU失败，选择的类别包含子级类别，不允许用于新增SPU！";
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,message);
        }

        Spu spu = new Spu();
        BeanUtils.copyProperties(addNewDTO,spu);
        spu.setId(IdUtils.generateId());
        spu.setBrandName(brandStandardVO.getName());
        spu.setCategoryName(categoryStandardVO.getName());
        spu.setSales(0);
        spu.setCommentCount(0);
        spu.setPositiveCommentCount(0);
        spu.setIsDeleted(0);
        spu.setIsPublished(0);
        spu.setIsNewArrival(1);
        spu.setIsChecked(1);
        spu.setCheckUser(null);
        spu.setGmtCheck(null);
        mapper.insert(spu);

        SpuDetail spuDetail = new SpuDetail();
        spuDetail.setSpuId(spu.getId());
        spuDetail.setDetail(addNewDTO.getDetail());
        detailMapper.insert(spuDetail);
    }
}
